package test5.lupeng.jf.com.teamdemo.adapter;


import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.bean.response.ResponseHome;
import test5.lupeng.jf.com.teamdemo.untils.GlideCircleTransform;

//动态列表的适配器
public class MessageAdapter extends RecyclerView.Adapter{


    ArrayList<ResponseHome.ResultBean> results;

    public MessageAdapter(ArrayList<ResponseHome.ResultBean> results){
        this.results = results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message,parent,false);
        HomeViewHolder holder = new HomeViewHolder(view);
        return holder;
    }

    public class HomeViewHolder extends RecyclerView.ViewHolder{

        private final ImageView iv_headImage;
        private final TextView tv_otherName;
        private final TextView tv_otherId;
        private final TextView tv_otherMessage;
        private final ImageView iv_sendImage;
        private final TextView first_count;
        private final TextView second_count;
        private final LinearLayout liner1;

        public HomeViewHolder(View itemView) {
            super(itemView);
            //头像
            iv_headImage = itemView.findViewById(R.id.iv_headImage);
            //用户名
            tv_otherName = itemView.findViewById(R.id.tv_otherName);
            //用户Id
            tv_otherId = itemView.findViewById(R.id.tv_otherId);
            //评论
            tv_otherMessage = itemView.findViewById(R.id.tv_otherMessage);
            //发表的图片
            iv_sendImage = itemView.findViewById(R.id.iv_sendImage);
            //笑脸的数量
            first_count = itemView.findViewById(R.id.first_count);
            //爱心的数量
            second_count = itemView.findViewById(R.id.second_count);
            liner1 = itemView.findViewById(R.id.liner1);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HomeViewHolder holder1 = (HomeViewHolder) holder;
        final ResponseHome.ResultBean bean = results.get(position);

        if (bean.getAuthor() != null) {

            Glide.with(holder1.itemView.getContext())
                    .load(bean.getAuthor().getHeadProtrait())
                    .centerCrop()
                    .transform(new GlideCircleTransform(holder1.itemView.getContext()))
                    .into(holder1.iv_headImage);

            holder1.tv_otherName.setText(bean.getAuthor().getUsername());

            //头像的点击事件
            holder1.iv_headImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    messageInterface.requestUserMessage(bean.getAuthor().getUserId());
                }
            });
        }

        //点击进入文章界面
        holder1.liner1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  //传一个内容url,文章id,
                  messageInterface.clickToTitle(bean.getContent(),bean.getInfoId(),bean.getCommentsSize(),bean.getLikeSize(),bean.isLikeSizeStatus());
            }
        });


        holder1.tv_otherId.setText(bean.getTimestamp() + "");
        holder1.tv_otherMessage.setText(bean.getSynopsis());

        Glide.with(holder1.itemView.getContext())
                .load(bean.getCoverImage())
                .centerCrop()
                .into(holder1.iv_sendImage);

        //评论数量
        holder1.first_count.setText(bean.getCommentsSize()+"");

        //收藏数量
        holder1.second_count.setText(bean.getLikeSize()+"");


    }

    @Override
    public int getItemCount() {
        return results.size();
    }


    MessageInterface messageInterface;

    public void setInterface(MessageInterface messageInterface) {
        this.messageInterface = messageInterface;
    }


    public interface MessageInterface{

        void requestUserMessage(String userId);

        //文章链接
        void clickToTitle(String content,String articleId,int count1,int count2,Boolean isLikeSizeStatus);

    }





}
